/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.net.greenet.common.db_service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import com.ambimmort.app.framework.uitls.Application;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import javax.sql.DataSource;

/**
 *
 * @author LiuShang
 */
public class BaseDao {

    private static final Logger LOG = Logger.getLogger(BaseDao.class.getName());
    public Connection conn = null;
    public PreparedStatement ps;
    public Statement stmt;
    public ResultSet rs;


    public boolean getConnection() {
        try {
            conn = ((DataSource)Application.getBean("dataSource")).getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

        return true;

    }

    // 增删改 delete from news_detail where id=? and title=?
    public int executeUpdate(String sql, Object[] params) {
        getConnection();
        int updateRows = 0;
        try {
            ps = conn.prepareStatement(sql);
            //填充占位符
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
            updateRows = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            rollback();
        }
        return updateRows;
    }
    
    public boolean excute(String sql){
        boolean flag = true;
        try {
            ps = conn.prepareStatement(sql);
            flag = ps.execute();
        } catch (SQLException ex) {
        }
        return flag;
    }

    // 查询
    public ResultSet executeSQL(String sql, Object[] params) {
        getConnection();
        try {
            ps = conn.prepareStatement(sql);
            //填充占位符
            for (int i = 0; i < params.length; i++) {
                ps.setObject(i + 1, params[i]);
            }
            rs = ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }
    public void insert(String sql, List<String> list) {
        long a = System.currentTimeMillis();
        try {
            ps = conn.prepareStatement(sql);
            final int batchSize = 2000;
            int count = 0;
            for (String bean : list) {
                String[] para = bean.split(",");
                for (int i = 0; i < para.length; i++) {
                    ps.setObject(i + 1, para[i]);
                }
                ps.addBatch();
                if (++count % batchSize == 0) {
                    ps.executeBatch();
                }
            }
            ps.executeBatch(); // insert remaining records
            conn.commit();
        } catch (SQLException ex) {
            LOG.error("excute " + ps.toString() + " failed! " + ex);
        }
        long b = System.currentTimeMillis();
        LOG.info("insert() " + list.size() + " records costs " + (b - a) + "ms.");
    }

    //验证批量操作
    public boolean array(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] < 1) {
                rollback();
                return false;
            }
        }
        return true;
    }

    // 关闭资源
    public boolean close() {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    public void rollback() {
        try {
            conn.rollback();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    public static void main(String[] args) {
        BaseDao db = new BaseDao();
        boolean flag = db.getConnection();
        System.out.println("conn=" + flag);
        db.close();
    }
}
